package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.EnumUtils;
import com.sxbbc.common.basics.utils.statics.FunctionUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.BBusiness;
import com.sxbbc.common.core.entity.jz.ChickenCoupon;
import com.sxbbc.common.core.entity.jz.ChickenSubscribe;
import com.sxbbc.common.core.enums.BusinessScopeEnums;
import com.sxbbc.common.core.enums.ChickenCouponStatusEnums;
import com.sxbbc.common.core.enums.ChickenSubscribeStatusEnums;
import com.sxbbc.common.core.mapper.jz.BBusinessMapper;
import com.sxbbc.common.core.mapper.jz.ChickenCouponMapper;
import com.sxbbc.common.core.mapper.jz.ChickenSubscribeMapper;
import com.sxbbc.common.core.service.IChickenSubscribeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.utils.BasePage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 预约提鸡 服务实现类
 * </p>
 *
 * @author
 * @since 2020-03-27
 */
@Service
public class ChickenSubscribeServiceImpl extends ServiceImpl<ChickenSubscribeMapper, ChickenSubscribe> implements IChickenSubscribeService {
    @Autowired
    private BBusinessMapper businessMapper;
    @Autowired
    private ChickenSubscribeMapper chickenSubscribeMapper;

    @Autowired
    private ChickenCouponMapper chickenCouponMapper;

    @Override
    public IPage<ChickenSubscribe> selectChickenSubscribeList(BasePage<ChickenSubscribe> page, ChickenSubscribe chickenSubscribe) {
        if (StringUtils.isNotBlank(chickenSubscribe.getQueryTime())) {
            String[] times = chickenSubscribe.getQueryTime().split("~");
            chickenSubscribe.setStart(times[0]);
            chickenSubscribe.setEnd(times[1]);
        }
        IPage<ChickenSubscribe> list = chickenSubscribeMapper.selectChickenSubscribeList(page,chickenSubscribe);

        if(list.getSize()>0){
            for (ChickenSubscribe record : list.getRecords()) {
                List<Integer> ids = FunctionUtils.getIntegerList(record.getCcids(),",");
                QueryWrapper<ChickenCoupon> queryWrapper = new QueryWrapper<>();
                queryWrapper.in("id",ids);
                List<ChickenCoupon> chickenCouponList = chickenCouponMapper.selectList(queryWrapper);
                List<String> couponTitleList = chickenCouponList.stream().map(ChickenCoupon::getTitle).collect(Collectors.toList());
                String titles = StringUtils.join(couponTitleList.toArray(), ",");
                record.setCcTitles(titles);
                record.setStatusName(EnumUtils.getEnum(ChickenSubscribeStatusEnums.class, EnumUtils.GET_ID, record.getStatus()).getName());
                record.setOnOffName(EnumUtils.getEnum(BusinessScopeEnums.class, EnumUtils.GET_ID, record.getOnOff()).getName());
                record.setNum(couponTitleList.size());
            }
        }
        return list;
    }

    @Override
    public RestResponse subscribeOrderVerifyInfo(Integer cid, String orderCode) {
        // 店铺信息
        QueryWrapper<BBusiness> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cid", cid);
        BBusiness business = businessMapper.selectOne(queryWrapper);
        if (business == null) {
            return GetRest.getFail("店铺信息有误");
        }
        CalcuUtils calcuUtils = CalcuUtils.getInstance();
        if (!calcuUtils.isEquals(StaticUtils.STATUS_SUCCESS, business.getStatus())
                && !calcuUtils.isEquals(StaticUtils.STATUS_SUCCESS, business.getOfflineStatus())) {
            return GetRest.getFail("店铺未通过审核");
        }

        QueryWrapper<ChickenSubscribe> subscribeQueryWrapper = new QueryWrapper<>();
        subscribeQueryWrapper.eq("order_code",orderCode);
        ChickenSubscribe chickenSubscribe = chickenSubscribeMapper.selectOne(subscribeQueryWrapper);
        if(null == chickenSubscribe){
            return GetRest.getFail("请输入正确的核销码");
        }
        if(CalcuUtils.getInstance().isEquals(business.getId(),chickenSubscribe.getBid())){
            return GetRest.getSuccess("", chickenSubscribe);
        }else{
            return GetRest.getFail("你不是该订单的商家,无法核销");
        }
    }

    @Override
    public RestResponse subscribeOrderVerify(Integer cid, String no) {
        CalcuUtils calcuUtils = CalcuUtils.getInstance();
        // 店铺信息
        QueryWrapper<BBusiness> queryWrapper = new QueryWrapper<BBusiness>();
        queryWrapper.eq("cid", cid);
        BBusiness business = businessMapper.selectOne(queryWrapper);
        if (business == null) {
            return GetRest.getFail("店铺信息有误");
        }
        if (!calcuUtils.isEquals(StaticUtils.STATUS_SUCCESS, business.getStatus())
                && !calcuUtils.isEquals(StaticUtils.STATUS_SUCCESS, business.getOfflineStatus())) {
            return GetRest.getFail("店铺未通过审核");
        }
        QueryWrapper<ChickenSubscribe> subscribeQueryWrapper = new QueryWrapper<>();
        subscribeQueryWrapper.eq("order_code",no);
        ChickenSubscribe vo = chickenSubscribeMapper.selectOne(subscribeQueryWrapper);
        if(null == vo){
            return GetRest.getFail("请输入正确的核销码");
        }
        if(!calcuUtils.isEquals(vo.getStatus(), ChickenSubscribeStatusEnums.SUBSCRIBE.getId())){
            return GetRest.getFail("该订单状态不能核销");
        }

        //判断店铺id和核销码是否正确。正确给与核销
        if(calcuUtils.isEquals(vo.getBid(),business.getId()) &&vo.getOrderCode().equals(no)){
            ChickenSubscribe subscribe = new ChickenSubscribe();
            subscribe.setId(vo.getId());
            subscribe.setStatus(ChickenSubscribeStatusEnums.USED.getId());
            subscribe.setCompleteTime(LocalDateTime.now());
            int i = chickenSubscribeMapper.updateById(subscribe);

            List<Integer> couponIds = FunctionUtils.getIntegerList(vo.getCcids().split(","));

            QueryWrapper<ChickenCoupon> wrapper = new QueryWrapper<>();
            wrapper.in("id",couponIds);

            ChickenCoupon coupon = new ChickenCoupon();
            coupon.setStatus(ChickenCouponStatusEnums.USED.getId());
            chickenCouponMapper.update(coupon,wrapper);
            if(i<1){
                return GetRest.getFail("核销失败，请重新再试");
            }
            return GetRest.getSuccess("操作成功");
        }
        return GetRest.getFail("你不是该订单商家，无法核销");
    }
}
